/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.tonetn.com
 */
package com.xunyin.modules.mp.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.google.common.collect.Maps;
import com.xunyin.enums.PayMethodEnum;
import com.xunyin.modules.mtstore.domain.MtStore;
import com.xunyin.modules.mtstore.service.StoreService;
import com.xunyin.utils.RedisUtil;
import com.xunyin.utils.RedisUtils;
import com.xunyin.utils.ShopKeyUtils;
import com.xunyin.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * 支付配置
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2020/03/01
 */
@Configuration(proxyBeanMethods = false)
public class WxPayConfiguration {
    private static final Logger log = LoggerFactory.getLogger(WxPayConfiguration.class);

    private static Map<String, WxPayService> payServices = Maps.newHashMap();

    private static RedisUtils redisUtils;
    private static StoreService storeService;

    /**
     * 店铺服务接口
     */
/*	@Autowired
	public StoreService storeService;*/
    @Autowired
    public WxPayConfiguration(RedisUtils redisUtils, StoreService storeService) {

        WxPayConfiguration.redisUtils = redisUtils;
        WxPayConfiguration.storeService = storeService;
    }

    /**
     *  获取WxPayService
     * @return
     */
    public static WxPayService getPayService(Integer storeId, PayMethodEnum payMethodEnum) {
        WxPayService wxPayService = payServices.get(ShopKeyUtils.getYshopWeiXinPayService() + payMethodEnum.getValue() + storeId);
        if (wxPayService == null || redisUtils.get(ShopKeyUtils.getYshopWeiXinPayService()) == null) {
            WxPayConfig payConfig = new WxPayConfig();
            switch (payMethodEnum) {
                case WECHAT:
                    payConfig.setAppId(redisUtils.getY(ShopKeyUtils.getWechatAppId()));
                    break;
                case WXAPP:
                    payConfig.setAppId(RedisUtil.get(ShopKeyUtils.getWxAppAppId()));
                    break;
                case APP:
                    payConfig.setAppId(RedisUtil.get(ShopKeyUtils.getWxNativeAppAppId()));
                    break;
                default:
            }
            payConfig.setMchId(redisUtils.getY(ShopKeyUtils.getWxPayMchId()));
            payConfig.setMchKey(redisUtils.getY(ShopKeyUtils.getWxPayMchKey()));
            payConfig.setKeyPath(redisUtils.getY(ShopKeyUtils.getWxPayKeyPath()));
//            payConfig.setPrivateKeyPath("/home/share2/file/other/apiclient_key.pem");
//            payConfig.setPrivateCertPath("/home/share2/file/other/apiclient_cert.pem");
//            payConfig.setPrivateKeyPath("https://api.jd8889.cn/api/file/other/apiclient_key.pem");
//            payConfig.setPrivateCertPath("https://api.jd8889.cn/api/file/other/apiclient_cert.pem");
//            payConfig.setApiV3Key(redisUtils.getY(ShopKeyUtils.getWxPayMchKey()));
            MtStore mtStore = storeService.queryStoreById(storeId);
            if (mtStore != null && StringUtil.isNotEmpty(mtStore.getWxApiV2()) && StringUtil.isNotEmpty(mtStore.getWxMchId())) {
                payConfig.setMchId(mtStore.getWxMchId());
                payConfig.setMchKey(mtStore.getWxApiV2());
                //pyConfig.setMchId(redisUtils.getY(mtStore.getWxMchId()));
                //payConfig.setMchKey(redisUtils.getY(mtStore.getWxApiV2()));
            }

            // 可以指定是否使用沙箱环境
            payConfig.setUseSandboxEnv(false);
            wxPayService = new WxPayServiceImpl();
            wxPayService.setConfig(payConfig);
            payServices.put(ShopKeyUtils.getYshopWeiXinPayService() + payMethodEnum.getValue() + storeId, wxPayService);

            //增加标识
            redisUtils.set(ShopKeyUtils.getYshopWeiXinPayService(), "xunyintech");
        }
        return wxPayService;
    }

//	/**
//	 *  获取小程序WxAppPayService
//	 * @return
//	 */
//	public static WxPayService getWxAppPayService() {
//		WxPayService wxPayService = payServices.get(ShopKeyUtils.getYshopWeiXinMiniPayService());
//		if(wxPayService == null || RedisUtil.get(ShopKeyUtils.getYshopWeiXinPayService()) == null) {
//			WxPayConfig payConfig = new WxPayConfig();
//			payConfig.setAppId(RedisUtil.get(ShopKeyUtils.getWxAppAppId()));
//			payConfig.setMchId(RedisUtil.get(ShopKeyUtils.getWxPayMchId()));
//			payConfig.setMchKey(RedisUtil.get(ShopKeyUtils.getWxPayMchKey()));
//			payConfig.setKeyPath(RedisUtil.get(ShopKeyUtils.getWxPayKeyPath()));
//			// 可以指定是否使用沙箱环境
//			payConfig.setUseSandboxEnv(false);
//			wxPayService = new WxPayServiceImpl();
//			wxPayService.setConfig(payConfig);
//			payServices.put(ShopKeyUtils.getYshopWeiXinMiniPayService(), wxPayService);
//
//			//增加标识
//			RedisUtil.set(ShopKeyUtils.getYshopWeiXinPayService(),"yshop");
//		}
//		return wxPayService;
//	}

//	/**
//	 *  获取APPPayService
//	 * @return
//	 */
//	public static WxPayService getAppPayService() {
//		WxPayService wxPayService = payServices.get(ShopKeyUtils.getYshopWeiXinAppPayService());
//		if(wxPayService == null || RedisUtil.get(ShopKeyUtils.getYshopWeiXinPayService()) == null) {
//			WxPayConfig payConfig = new WxPayConfig();
//			payConfig.setAppId(RedisUtil.get(ShopKeyUtils.getWxNativeAppAppId()));
//			payConfig.setMchId(RedisUtil.get(ShopKeyUtils.getWxPayMchId()));
//			payConfig.setMchKey(RedisUtil.get(ShopKeyUtils.getWxPayMchKey()));
//			payConfig.setKeyPath(RedisUtil.get(ShopKeyUtils.getWxPayKeyPath()));
//			// 可以指定是否使用沙箱环境
//			payConfig.setUseSandboxEnv(false);
//			wxPayService = new WxPayServiceImpl();
//			wxPayService.setConfig(payConfig);
//			payServices.put(ShopKeyUtils.getYshopWeiXinAppPayService(), wxPayService);
//
//			//增加标识
//			RedisUtil.set(ShopKeyUtils.getYshopWeiXinPayService(),"yshop");
//		}
//		return wxPayService;
//	}

    /**
     * 移除WxPayService
     */
    public static void removeWxPayService() {
        redisUtils.del(ShopKeyUtils.getYshopWeiXinPayService());
        payServices.remove(ShopKeyUtils.getYshopWeiXinPayService());
        //payServices.remove(ShopKeyUtils.getYshopWeiXinMiniPayService());
        //payServices.remove(ShopKeyUtils.getYshopWeiXinAppPayService());
    }

}
